//
//  AppDelegate.m
//  PigMarket
//
//  Created by Felix on 2019/3/4.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "AppDelegate.h"
#import "MainTabBarController.h"
#import <UMCommon/UMCommon.h>
#import "IQKeyboardManager.h"
#import <AlipaySDK/AlipaySDK.h>
#import <AMapFoundationKit/AMapFoundationKit.h>
#import "TJMainTabBarViewController.h"
#import "BaseNavViewController.h"

//#import <EZOpenSDKFramework/EZOpenSDKFramework.h>
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    //监控全屏设置
    self.allowRotation = NO;
    
    //监控
    //[self configPlayer];
    
    //key
    [self configKeyBored];
    
    //U-Share平台设置
    [self configUShareSettings];
    [self configUSharePlatforms];
    
    //微信支付
    [self configWXPay];
    
    //地图
    [self configMap];
    
    //聊天登录
    //[self configIM];
    
    [self initWindow];
    
    return YES;
}


//MARK: 初始化 window
-(void)initWindow{
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = KWhiteColor;
    [self initUserManager];
    [self.window makeKeyAndVisible];
}

//MARK: 初始化用户系统
- (void)initUserManager{
    
    TJMainTabBarViewController *vc = [[TJMainTabBarViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    self.window.rootViewController = nav;
}


#pragma mark - IM
- (void)configIM {
    UserInfo *userInfo = [UserInfoDataProvider sharedInstance].userInfo;
    
    if (![JTool iSNull:userInfo.memberToken]) {
        //IM登录
        [[ChatManager sharedInstance] loginIMWithUserName:userInfo.memberNum userSig:userInfo.userSig succ:^{
            userInfo.isIMLogin = YES;
            [UserInfoDataProvider sharedInstance].userInfo = userInfo;
            PMLog(@"聊天IM成功");
            [[AlertAction sharedInstance] showAlertWithTitle:@"123" message:@"IMLogin成功" confirmBtnTitle:@"ok" confirmBlock:nil cancelBlock:nil];

        } fail:^(int code, NSString * _Nonnull msg) {
            PMLog(@"聊天IM失败");
            [[AlertAction sharedInstance] showAlertWithTitle:@"123" message:@"IMLogin失败" confirmBtnTitle:@"ok" confirmBlock:nil cancelBlock:nil];
        }];
    }
}


#pragma mark - 监控配置
- (void)configPlayer {
//    NSString *appKey = @"bcdf5325fe0f4491babc142403a76109";
//    [EZOpenSDK initLibWithAppKey:appKey];
//    [EZHCNetDeviceSDK initSDK];
}


#pragma mark - 键盘配置
-(void)configKeyBored {
    
    IQKeyboardManager *manager = [IQKeyboardManager sharedManager];
    manager.enable = YES;
    manager.shouldResignOnTouchOutside = YES;
    manager.shouldToolbarUsesTextFieldTintColor = YES;
    manager.enableAutoToolbar = NO;
}


#pragma mark - 友盟配置
- (void)configUShareSettings {
    [UMConfigure initWithAppkey:@"5c7de7ad61f5647fd8000e58" channel:@"App Store"];
    
    //    [UMConfigure setLogEnabled:YES];
    //打开图片水印
    [UMSocialGlobal shareInstance].isUsingWaterMark = YES;
    
    /*
     * 关闭强制验证https，可允许http图片分享，但需要在info.plist设置安全域名
     <key>NSAppTransportSecurity</key>
     <dict>
     <key>NSAllowsArbitraryLoads</key>
     <true/>
     </dict>
     */
    //[UMSocialGlobal shareInstance].isUsingHttpsWhenShareContent = NO;
}


#pragma mark - 三方登录配置
- (void)configUSharePlatforms {
    
    /* 设置微信的appKey和appSecret */
     [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:@"wx97793237fbc3485a" appSecret:@"16b9aede7862723410b2e9795a80b79d" redirectURL:@"http://mobile.umeng.com/social"];
    
    /* 设置分享到QQ互联的appID
     * U-Share SDK为了兼容大部分平台命名，统一用appKey和appSecret进行参数设置，而QQ平台仅需将appID作为U-Share的appKey参数传进即可。
     */
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ appKey:@"1108261708"/*设置QQ平台的appID*/  appSecret:nil redirectURL:@"http://mobile.umeng.com/social"];
    
    /* 设置新浪的appKey和appSecret */
    [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Sina appKey:@"3921700954"  appSecret:@"04b48b094faeb16683c32669824ebdad" redirectURL:@"https://sns.whalecloud.com/sina2/callback"];
    
    /* 支付宝的appKey */
    [[UMSocialManager defaultManager] setPlaform: UMSocialPlatformType_AlipaySession appKey:@"2019041963904965" appSecret:nil redirectURL:@"http://mobile.umeng.com/social"];
}


#pragma mark - 注册微信
- (void)configWXPay {
    [[PayManager sharedManager] wxPayRegister];
}


#pragma mark - 地图配置
- (void)configMap {
    NSString *appkey = @"ef9d5f165372ba1dad23d2d83bff14ee";
    [AMapServices sharedServices].apiKey = appkey;
}


#pragma mark - UShare设置系统回调 仅支持9.0以上系统
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options
{
    //6.3的新的API调用，是为了兼容国外平台(例如:新版facebookSDK,VK等)的调用[如果用6.2的api调用会没有回调],对国内平台没有影响
    BOOL result = [[UMSocialManager defaultManager]  handleOpenURL:url options:options];
    if (!result) {
        
        //处理微信 支付宝 支付回调
        [[PayManager sharedManager] handelPayURL:url];
    }
    return result;
}


- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    if (self.allowRotation) {
        return UIInterfaceOrientationMaskLandscapeRight;
    }
    return UIInterfaceOrientationMaskPortrait;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
